package net.gz01.wallet.web.controller.admin;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.util.ZODate;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Merch;
import net.gz01.wallet.web.model.YouliAccount;
import net.gz01.wallet.web.model.YouliAccountRule;

public class YouliAccountRuleController extends BaseController {

	public void search() {
		render("basedata/youliAccountRuleList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<YouliAccountRule> pageResult = YouliAccountRule.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void addPage() {
		setAttr("youliAccountList", YouliAccount.dao.selectAll());
		render("basedata/youliAccountRuleAdd.html");
	}

	@Before(Tx.class)
	public void add() {
		String name = getPara("name");
		if (StrKit.isBlank(name)) {
			renderFail("规则名称必填");
			return;
		}
		List<YouliAccount> youliAccountList = YouliAccount.dao.selectAll();
		int size = youliAccountList.isEmpty() ? 0 : youliAccountList.size();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			String paraName = "youliAccountId[" + i + "]";
			String paraVale = getPara(paraName);
			if (StrKit.notBlank(paraVale)) {
				sb.append(paraVale).append(",");
			}

		}
		if (StrKit.isBlank(sb.toString())) {
			renderFail("银行账户必选");
			return;
		}

		String accountId = "";
		if (StrKit.notBlank(sb.toString())) {
			accountId = sb.toString();
			accountId = accountId.substring(0, accountId.length() - 1);
		}
		YouliAccountRule youliAccountRule = new YouliAccountRule();
		youliAccountRule.setName(name);
		youliAccountRule.setYouliAccountId(accountId);
		youliAccountRule.setUpdateTime(ZODate.getDateTime());
		youliAccountRule.save();
		renderSuccess();
	}

	public void updatePage() {
		YouliAccountRule youliAccountRule = YouliAccountRule.dao.findById(getPara("id"));
		String[] accountArray = youliAccountRule.getYouliAccountId().split(",");
		List<YouliAccount> youliAccountList = YouliAccount.dao.selectAll();
		for (YouliAccount a : youliAccountList) {
			a.setIsStop(false);
			for (String s : accountArray) {
				if (s.equals(a.getId().toString())) {
					a.setIsStop(true);
				}
			}
		}
		setAttr("youliAccountRule", youliAccountRule);
		setAttr("youliAccountList", youliAccountList);
		render("basedata/youliAccountRuleUpdate.html");
	}

	@Before(Tx.class)
	public void update() {
		YouliAccountRule youliAccountRule = getModel(YouliAccountRule.class);
		List<YouliAccount> youliAccountList = YouliAccount.dao.selectAll();
		int size = youliAccountList.isEmpty() ? 0 : youliAccountList.size();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			String paraName = "youliAccountId[" + i + "]";
			String paraVale = getPara(paraName);
			if (StrKit.notBlank(paraVale)) {
				sb.append(paraVale).append(",");
			}

		}
		if (StrKit.isBlank(sb.toString())) {
			renderFail("银行账户必选");
			return;
		}

		String accountId = "";
		if (StrKit.notBlank(sb.toString())) {
			accountId = sb.toString();
			accountId = accountId.substring(0, accountId.length() - 1);
		}
		youliAccountRule.setYouliAccountId(accountId);
		youliAccountRule.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete() {
		if(Merch.dao.isExistByRoleId(getPara("id"))){
			String msg = "该规则已被引用，如需删除，请前往钱包管理-用户管理更新用户的收款账户规则！";
			renderSuccess(msg,false);

		}else{
			YouliAccountRule.dao.deleteById(getPara("id"));
			renderSuccess(false);
		}

	}

}
